package com.loeng.module.system.controller.admin.user;

import com.loeng.framework.apilog.core.annotation.ApiAccessLog;
import com.loeng.framework.common.enums.CommonStatusEnum;
import com.loeng.framework.common.pojo.CommonResult;
import com.loeng.framework.common.pojo.PageParam;
import com.loeng.framework.common.pojo.PageResult;
import com.loeng.framework.common.util.object.BeanUtils;
import com.loeng.framework.excel.util.ExcelUtils;
import com.loeng.module.system.controller.admin.user.vo.UserSimpleRespVO;
import com.loeng.module.system.controller.admin.user.vo.UsersPageReqVO;
import com.loeng.module.system.controller.admin.user.vo.UsersRespVO;
import com.loeng.module.system.controller.admin.user.vo.UsersSaveReqVO;
import com.loeng.module.system.dal.dataobject.user.AdminUserDO;
import com.loeng.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.loeng.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.loeng.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 用户信息")
@RestController
@RequestMapping("/system/user")
@Validated
public class UsersController {

    @Resource
    private AdminUserService usersService;

    @PostMapping("/create")
    @Operation(summary = "创建用户信息")
    @PreAuthorize("@ss.hasPermission('system:users:create')")
    public CommonResult<Long> createUsers(@Valid @RequestBody UsersSaveReqVO createReqVO) {
        return success(usersService.createUsers(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户信息")
    @PreAuthorize("@ss.hasPermission('system:users:update')")
    public CommonResult<Boolean> updateUsers(@Valid @RequestBody UsersSaveReqVO updateReqVO) {
        usersService.updateUsers(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:users:delete')")
    public CommonResult<Boolean> deleteUsers(@RequestParam("id") Long id) {
        usersService.deleteUsers(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:users:query')")
    public CommonResult<UsersRespVO> getUsers(@RequestParam("id") Long id) {
        AdminUserDO users = usersService.getUsers(id);
        return success(BeanUtils.toBean(users, UsersRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户信息分页")
    @PreAuthorize("@ss.hasPermission('system:users:query')")
    public CommonResult<PageResult<UsersRespVO>> getUsersPage(@Valid UsersPageReqVO pageReqVO) {
        PageResult<AdminUserDO> pageResult = usersService.getUsersPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UsersRespVO.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取用户精简信息列表", description = "只包含被开启的用户，主要用于前端的下拉选项")
    public CommonResult<List<UserSimpleRespVO>> getSimpleUserList() {
        // todo 目前只查了用户信息，没有查询其对应的部门信息
        List<AdminUserDO> list = usersService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(BeanUtils.toBean(list, UserSimpleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:users:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUsersExcel(@Valid UsersPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AdminUserDO> list = usersService.getUsersPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户信息.xls", "数据", UsersRespVO.class,
                        BeanUtils.toBean(list, UsersRespVO.class));
    }

}